package com.xintonghua.bussiness.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.ui.view.DrawView;
import com.xintonghua.bussiness.util.ImageTools;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liqi on 2018/4/2.
 */

public class DialogQueRenQianZiActivity extends BaseActivity {

    @BindView(R.id.dv_drawview)
    DrawView dvDrawview;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.tv_title_m)
    TextView tvTitleM;
    @BindView(R.id.tv_msg_m)
    TextView tvMsgM;

    @BindView(R.id.tv_qianziquyu)
    TextView tv_qianziquyu;

    String title;
    String msg;
    @BindView(R.id.iv_chacha)
    ImageView ivChacha;

    ShopOrderListBean.ListBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_querendaodian);
        ButterKnife.bind(this);
        title = JumpUtils.getString(this, "title");
        msg = JumpUtils.getString(this, "msg");
        bean = (ShopOrderListBean.ListBean) JumpUtils.getSerializable(this);
        if (!TextUtils.isEmpty(title)) {
            tvTitleM.setText("" + title);
        }

        if (!TextUtils.isEmpty(msg)) {
            tvMsgM.setVisibility(View.VISIBLE);
            tvMsgM.setText("" + msg);
        }
        dvDrawview.setIsCanDraw(false);
        dvDrawview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HashMap<String, String> map = new HashMap<>();
                map.put("title", "工作人员签字");

                JumpUtils.jumpActivityForResult(DialogQueRenQianZiActivity.this, DialogMemeberSignActivity.class, map, MyState.requestL3000);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestL3000 && resultCode == Activity.RESULT_OK) {

            if (data != null) {
                Log.i("aaa", "數據===》" + data.getExtras().getSerializable("data"));
                List<Point> pointList = (List<Point>) data.getSerializableExtra("data");
                Log.i("aaa", "目前list大小===》" + pointList.size());
                dvDrawview.setPlist(pointList);
                tv_qianziquyu.setVisibility(View.GONE);
            }


        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestFour:
                    try {
                        String datas = data.toString();

                        httpCent.getReserve("" + bean.getId(), "" + AuthManager.geteAuth().getId(), datas, DialogQueRenQianZiActivity.this, MyState.requestFive);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("aaa", "错误");
                    }
                    break;
                case MyState.requestFive:
                    try {
                        MyUtils.mToast(DialogQueRenQianZiActivity.this, "确认成功");
//                        new IntentFilter(MyState.BROAD_CASET_RECEIVER_REFRESH_HOME));
                        Intent mIntent = new Intent(MyState.BROAD_CASET_RECEIVER_REFRESH_HOME);
//                        mIntent.putExtra("message", "hahahahaha");
                        sendBroadcast(mIntent);
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("aaa", "错误");
                    }

                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Bitmap ChangeBitmap(List<Point> plist, int widths, int heights) {
        int width;
        int height;
        width = widths;
        height = heights;

        Log.i("aaa", "canvas-----> width====>" + width + "//height===>" + height);
        //创建一个bitmap
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);// 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布\
        Canvas canvas = new Canvas(newb);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawRect(new Rect(0, 0, width, height), paint);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        for (int i = 0; i < plist.size() - 1; i++) {
            if (plist.get(i).x > 0 && plist.get(i).y > 0 && plist.get(i + 1).x > 0 && plist.get(i + 1).y > 0)
                canvas.drawLine(plist.get(i).x, plist.get(i).y, plist.get(i + 1).x, plist.get(i + 1).y, paint);
        }
        // 保存
        canvas.save();
        // 存储
        canvas.restore();
        return newb;
    }

    @OnClick({R.id.iv_chacha, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_chacha:
                finish();
                break;
            case R.id.btn_ok:
                submit();
                break;
        }
    }

    public void submit() {
        if (MyUtils.isNullOrEmpty(dvDrawview.getPlist())) {
            MyUtils.mToast(DialogQueRenQianZiActivity.this, "请工作人员签字");
            return;
        }
        if (dvDrawview.getPlist() != null) {
            List<Point> pointList = dvDrawview.getPlist();
            int width = dvDrawview.getWidth();
            int height = dvDrawview.getHeight();
            Bitmap bmp = ChangeBitmap(pointList, width, height);
            Log.i("aaa", "目前list大小===》" + pointList.size());
            String bmp1filename = getApplicationContext().getFilesDir().getAbsolutePath() + "/bmp" + System.currentTimeMillis();
            ImageTools.savePhotoToSDCard(bmp, bmp1filename);
            File file1 = new File(bmp1filename);

            if (!file1.exists()) {
                MyUtils.mToast(DialogQueRenQianZiActivity.this, "保存图片失败,请检查权限");
                return;
            }
            ImageTools.loadImage(DialogQueRenQianZiActivity.this, file1.getAbsolutePath(), this, MyState.requestFour);
        }
    }

}
